import datetime
from django.shortcuts import render
from . import models
from django.views import View
from django.http import HttpResponse, JsonResponse

from . import data_method

# Create your views here.

class ProductType(View):

    def get(self, request):

        limit = request.GET.get("limit")

        offset = request.GET.get("offset")

        if limit == None:

            limit = 20
        else: 
            limit = int(limit)
        
        if offset == None:

            offset = 0
        else:
            offset = int(offset)

        result_list = []

        result = models.ProductType.objects.all()[offset: limit]

        for i in result:

            result_list.append({"id": i.id, "product_type": i.product_type})

        return JsonResponse(
            {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": result_list
            }
        )

    def post(self, requst):

        product_type = requst.POST.get('product_type')

        p = models.ProductType(product_type=product_type)

        p.save()

        return JsonResponse(
            {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": p.id
            }
        )


class ProductTypeDetail(View):


    def get(self, request, pk):

        result = models.ProductType.objects.get(id=pk)

        return JsonResponse(
            {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": {
                    'id': result.id,
                    'product_type': result.product_type
                }
            }
        )

    def post(self, request, pk): # 修改

        res = models.ProductType.objects.get(id=pk)

        product_type = request.POST.get('product_type')

        res.product_type = product_type

        res.save()

        return JsonResponse(
            {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": "修改成功"
            }
        )

    def delete(self, request, pk): # 删除

        res = models.ProductType.objects.get(id=pk)

        res.delete()

        return JsonResponse(
            {
                "status": 1,
                "errorCode": None,
                "errorMsg": None,
                "resultBody": "删除成功"
            }
        )


def tasks_in_progress(request): # 安任务类型取出正在进行的任务数量

    return JsonResponse(data_method.tasks_in_progress())

def get_product_by_type(request, product_type_id): # 按任务类型取出product

    result = []

    product_list = models.Product.objects.filter(product_type__id=product_type_id)

    for product in product_list:

        info = {
            "id": product.id,
            "product": product.product,
            "product_code": product.product_code,
            "master": product.master.username,
            "goal": product.goal,
            "product_backlog_count": models.ProductBacklog.objects.filter(product__product_type__id=product_type_id).count(),
            "product_backlog_count_success_count": models.ProductBacklog.objects.filter(product__product_type__id=product_type_id, operation=3).count(),
            "product_backlog_count_in_progress": [i.product_backlog for i in models.ProductBacklog.objects.filter(product__product_type__id=product_type_id, operation=2)],
            "completion_date": product.completion_date
        }

        result.append(info)

    return JsonResponse({
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result
    })



def get_product_backlog_by_product_id(request, product_id): # 

    return JsonResponse(data_method.get_product_backlog_by_product_id(product_id))

def get_sprint_backlog_by_product_backlog_id(request, product_backlog_id): # 

    result = []

    sprint_backlog_list = models.SprintBacklog.objects.filter(product_backlog__id=product_backlog_id)

    for sprint_backlog in sprint_backlog_list:

        info = {
            "id": sprint_backlog.id,
            "sprint_backlog": sprint_backlog.sprint_backlog,
            "start_date": sprint_backlog.start_date,
            "end_date": sprint_backlog.end_date,
            "completion_date": sprint_backlog.completion_date,
            "operation": sprint_backlog.operation,

        }

        result.append(info)

    return JsonResponse({
        "status": 1,
        "errorCode": None,
        "errorMsg": None,
        "resultBody": result
    })


def get_sb_html(request, product_backlog_id):

    sprint_backlog_list = models.SprintBacklog.objects.filter(product_backlog__id=product_backlog_id)

    result = ""

    html_black = """

    <tr style="color:{};">
        <th scope="row">{}</th>
        <td>{}</td>
        <td>{} 到 {}</td>
        <td>{}</td>
        <td>
            {}
        </td>
    </tr>
    
    
    """

    for i in sprint_backlog_list:

        color = ''

        if i.operation in [1, 2]:

            if i.end_date.replace(tzinfo=None) < datetime.datetime.utcnow(): # 在进行的任务超时未完成

                color = 'red'

            elif i.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=1): # 在进行的任务还省不到1天时间

                color = '#ff6347'

            elif i.end_date.replace(tzinfo=None) < datetime.datetime.utcnow() + datetime.timedelta(days=3): # 在进行的任务还省不到3天时间
                
                color = 'green'

        if i.operation == 0:

            operation = "撤销"

        elif i.operation == 1:
            operation = "等待"

        elif i.operation == 2:

            operation = "进行中"

        elif i.operation == 3:

            operation = "完成"

        # 0 为撤销 1 为等待 2为进行中 3为完成


        result += html_black.format(
            color,
            i.id,
            i.sprint_backlog,
            i.start_date,
            i.end_date,
            i.completion_date,
            operation
        )

    return HttpResponse(result)








